//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
using System.Security.Cryptography;
using System.Text;
 
namespace STT.Business
{
    public class BTbDangNhap
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDangNhap_SelectTop", pr);
            return all;
        }
        public string GetMD5(string str)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

            StringBuilder sbHash = new StringBuilder();

            foreach (byte b in bHash)
            {

                sbHash.Append(String.Format("{0:x2}", b));
            }
            return sbHash.ToString();
        }
        public string Radomchar()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var stringChars = new char[8];
            var random = new Random();

            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }
            var finalString = new String(stringChars);
            return finalString.ToString();
            //
        }
//-----------------------------------------------------------//
        public static void Insert(ETbDangNhap OTbDangNhap)
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter(@"MaHV", OTbDangNhap.MaHV);
            pr[1] = new SqlParameter(@"MatKhau", OTbDangNhap.MatKhau);
            pr[2] = new SqlParameter(@"Quyen", OTbDangNhap.Quyen);
            pr[3] = new SqlParameter(@"RadomPass", OTbDangNhap.RadomPass);
            pr[4] = new SqlParameter(@"KhoaTaiKhoan", OTbDangNhap.KhoaTaiKhoan);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDangNhap_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbDangNhap OTbDangNhap)
        {
            SqlParameter[] pr = new SqlParameter[6];
            pr[0] = new SqlParameter(@"MaDN", OTbDangNhap.MaDN);
            pr[1] = new SqlParameter(@"MaHV", OTbDangNhap.MaHV);
            pr[2] = new SqlParameter(@"MatKhau", OTbDangNhap.MatKhau);
            pr[3] = new SqlParameter(@"Quyen", OTbDangNhap.Quyen);
            pr[4] = new SqlParameter(@"RadomPass", OTbDangNhap.RadomPass);
            pr[5] = new SqlParameter(@"KhoaTaiKhoan", OTbDangNhap.KhoaTaiKhoan);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDangNhap_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaDN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDN",MaDN);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDangNhap_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbDangNhap SelectByID(int MaDN)
        {
            ETbDangNhap OTbDangNhap = new ETbDangNhap();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDN",MaDN);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbDangNhap_SelectByID", pr);
            if (idr.Read())
              OTbDangNhap = GetOneTbDangNhap(idr);
            idr.Close();
            idr.Dispose();
            return OTbDangNhap;
        }
//-----------------------------------------------------------//
        private static ETbDangNhap GetOneTbDangNhap(IDataReader idr)
       {
            ETbDangNhap OTbDangNhap = new ETbDangNhap();
                if (idr["MaDN"] != DBNull.Value)
                    OTbDangNhap.MaDN = (int)idr["MaDN"];
                if (idr["MaHV"] != DBNull.Value)
                    OTbDangNhap.MaHV = (string)idr["MaHV"];
                if (idr["MatKhau"] != DBNull.Value)
                    OTbDangNhap.MatKhau = (string)idr["MatKhau"];
                if (idr["Quyen"] != DBNull.Value)
                    OTbDangNhap.Quyen = (string)idr["Quyen"];
                if (idr["RadomPass"] != DBNull.Value)
                    OTbDangNhap.RadomPass = (string)idr["RadomPass"];
                if (idr["KhoaTaiKhoan"] != DBNull.Value)
                    OTbDangNhap.KhoaTaiKhoan = (int)idr["KhoaTaiKhoan"];
            return OTbDangNhap;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaDN)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaDN",MaDN);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbDangNhap_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbDangNhap> ListAll()
        {
            List<ETbDangNhap> list = new List<ETbDangNhap>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDangNhap_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbDangNhap(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbDangNhap> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbDangNhap> list = new List<ETbDangNhap>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDangNhap_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbDangNhap(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}